package com.springboot.common;

import com.springboot.interceptor.JwtInterceptor;
import com.springboot.interceptor.RoleInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * 拦截器
 *
 * @author zjb
 * @since 2025-09-10 12:15
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private JwtInterceptor jwtInterceptor;

    @Resource
    private RoleInterceptor roleInterceptor;
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 指定controller统一的接口前缀
        configurer.addPathPrefix("/api", clazz -> clazz.isAnnotationPresent(RestController.class));
    }

    @Override
    public  void addInterceptors(InterceptorRegistry registry){

        //注册jwt权限拦截器
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns(
                        "/api/users/login",
                        "/api/users/register",
                        "/api/users/test",
                        "/api/category/hierarchy",
                        "/api/files/get/**",
                        "/api/carousel/list",
                        "/api/product/list",
                        "/api/product/**",
                        "/api/comment/list/**"
                );
        // 注册角色权限拦截器
        registry.addInterceptor(roleInterceptor)
                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns(    // 排除不需要拦截的路径
                        "/error",
                        "/users/login",
                        "/users/register",
                        "/api/files/**"
                );
    }


}